# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Saul Ibarra Corretge <saghul@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=p11-kit
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.23.20
pkgrel=2
pkgdesc="Library to work with PKCS#11 modules"
arch=('any')
url="https://p11-glue.freedesktop.org/p11-kit.html"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-libtasn1"
         "${MINGW_PACKAGE_PREFIX}-libffi"
         "${MINGW_PACKAGE_PREFIX}-gettext")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc")
options=('!debug' 'strip' 'staticlibs')
source=(https://github.com/p11-glue/p11-kit/releases/download/${pkgver}/${_realname}-${pkgver}.tar.xz{,.sig}
        0001-allow-gtkdocize.all.patch
        0002-fix-includes.patch
        0004-fix-abspath-check.all.patch
        0006-p11-kit-relocation.patch
        0007-mmap-CreateFile-with-FILE_SHARE_READ.patch
        0008-add-debugging-to-mmap.patch
        0009-add-debugging-to-path.patch
        0010-fix-transport-test.patch
        0011-p11-kit-spawn-external.patch
        0012-no-version-script.patch)
validpgpkeys=('C0F67099B808FB063E2C81117BFB1108D92765AF')
sha256sums=('14d86024c3dfd6b967d9bc0b4ec7b2973014fe7423481f4d230a1a63b8aa6104'
            'SKIP'
            '195b2e8695f701caf545e2a468383ad29457febd9b1ee57de1986de04ad3c31c'
            'aa92f986d3f7dfc119e86f9a8f3987e6ac2562149921820eadbe09218c4df99c'
            'ea4593324db6d2d193733561f0352c6d679d1fb00aed0a2bae0d28aecbe92721'
            '14d39c061d947c40a3354fbfc2a126d6d025ce78ed8fb1d68f36c15ad01a0621'
            '2b0fc5c54776aca54e306315338323075e7412d8d800f490a31870008c34f8d1'
            '652e4be637bf77f20d8fde91175286701430e720e4082e8818a50ebddff28460'
            '8c796ade0ef6356c796d2e87b967c7a9d888783c08e740e35d5686f6deb7f009'
            '6c4b94bb8aee1981c07215d2c8449614f1a604a705f05806f6f1e8299b729698'
            'd5e804ba795aefe0a3938bc527b0e6c8ff8c1d5dfa1c0c1728c20a249504af6b'
            '15a2729f39e260f7fe8d55a91a399f2512854da626a69bc215470f902fc178cd')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  rm -f common/pathtools.c common/pathtools.h > /dev/null 2>&1 || true
  patch -p1 -i ${srcdir}/0001-allow-gtkdocize.all.patch
  patch -p1 -i ${srcdir}/0002-fix-includes.patch
  patch -p1 -i ${srcdir}/0004-fix-abspath-check.all.patch
  patch -p1 -i ${srcdir}/0006-p11-kit-relocation.patch
  patch -p1 -i ${srcdir}/0007-mmap-CreateFile-with-FILE_SHARE_READ.patch
  # patch -p1 -i ${srcdir}/0007-add-debugging-to-mmap.patch
  # patch -p1 -i ${srcdir}/0008-add-debugging-to-path.patch
  patch -p1 -i ${srcdir}/0010-fix-transport-test.patch
  patch -p1 -i ${srcdir}/0011-p11-kit-spawn-external.patch
  patch -p1 -i ${srcdir}/0012-no-version-script.patch

  autoreconf -vfi
  gtkdocize
}

build() {
  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=( --enable-debug=yes )
  else
    extra_config+=( --enable-debug=no )
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  PKG_CONFIG_PATH="${PKG_CONFIG_PATH}:/usr/share/pkgconfig" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --localstatedir=${MINGW_PREFIX}/var \
    --with-trust-paths="${MINGW_PREFIX}/etc/pki/ca-trust/source:${MINGW_PREFIX}/share/pki/ca-trust-source" \
    --disable-silent-rules \
    "${extra_config[@]}"

  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=;-DP11_SYSTEM_CONFIG_FILE=;-DP11_SYSTEM_CONFIG_MODULES=;-DP11_PACKAGE_CONFIG_MODULES=;-DP11_MODULE_PATH=;-DDATADIR=;-DSYSCONFDIR=;-DPRIVATEDIR=;-DP11_USER_CONFIG_FILE=;-DP11_USER_CONFIG_MODULES=" \
    make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  MSYS2_ARG_CONV_EXCL="-DBINDIR=;-DDATA_DIR=;-DP11_SYSTEM_CONFIG_FILE=;-DP11_SYSTEM_CONFIG_MODULES=;-DP11_PACKAGE_CONFIG_MODULES=;-DP11_MODULE_PATH=;-DDATADIR=;-DSYSCONFDIR=;-DPRIVATEDIR=;-DP11_USER_CONFIG_FILE=;-DP11_USER_CONFIG_MODULES=" \
    make DESTDIR="${pkgdir}" install
}
